import Taro from "@tarojs/taro";

// 定义返回结果的接口
interface LocationResult {
    latitude: number;
    longitude: number;

}

const getLocation = (callback) => {
    if (Taro.env.TARO_ENV === 'h5') {

        if ("geolocation" in navigator) {
            const location: LocationResult = {
                latitude: 0,
                longitude: 0
            };
            navigator.geolocation.getCurrentPosition((position) => {
                location.latitude = position.coords.latitude;
                location.longitude = position.coords.longitude;
                console.log(`纬度: ${position.coords.latitude}, 经度: ${position.coords.longitude}`);
                callback(location);
            }, (error) => {
                callback(location);
                console.error('获取位置时出错:', error);
            });
        } else {

            console.log("此浏览器不支持地理位置功能。");
        }
    } else {
        Taro.getLocation({
            type: 'gcj02' // 返回可以用于 wx.openLocation 的经纬度
        }).then(res => {
            callback(res);
        });
    }


};
export default getLocation;
